package com.easy.goods.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.Goods;
import com.easy.common.service.IGoodsService;
import com.easy.goods.mapper.GoodsMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@DubboService
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {
    @Override
    public Goods getGoodsById(Integer id) {
        return super.getById(id);
    }

    @Override
    public Goods getGoodsByName(String name) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Goods::getName, name);
        return super.getOne(queryWrapper);
   }

    @Override
    public int addGoods(Goods goods) {
        return super.save( goods)?1:0;
    }

    @Override
    public int updateGoods(Goods goods) {
        return super.updateById(goods)?1:0;
    }

    @Override
    public int deleteGoods(Integer id) {
        return super.removeById(id)?1:0;
    }

    @Override
    public int reverseStatus(Integer id) {
        Goods goods = super.getById(id);
        if (goods != null) {
            goods.setStatus(goods.getStatus() == 1 ? 0 : 1);
        }
        if (super.updateById(goods)) {
            return 1;
        }
        return 0;
    }

    @Override
    public List<Goods> getAll(Goods goods) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Goods::getName, goods.getName());
        return super.list(queryWrapper);
    }

    @Override
    public IPage<Goods> getPage(Goods goods, IPage page) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Goods::getName, goods.getName());
        return super.page( page);
    }
}
